﻿
using SqlSugar.Extensions;

namespace TestPressure
{
    public partial class TemplateAddForm : BaseForm
    {
        #region 属性

        private Template obj = new Template();

        #endregion

        #region 自定义方法

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["template_number"] = TemplateNumberInput.Text;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            int count = list.Count;
            if (count > 0)
            {
                Error(where["template_number"] + "模板已存在",this);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }

        #endregion

        #region 系统方法
        public TemplateAddForm()
        {
            InitializeComponent();

        }

        private void AddTemplateForm_Load(object sender, EventArgs e)
        {
            this.Header.Text = "添加模板";
            Size size = new Size(int.Parse((747 * this.Dpi()).ToString()), int.Parse((633 * this.Dpi()).ToString()));
            this.MinimumSize = size;
            this.MaximumSize = size;
        }
        #endregion

        #region 点击事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (TemplateNumberInput.Text.Length == 0)
            {
                Error("请输入模板编号", this);
                TemplateNumberInput.Focus();
                return;
            }

            if (PipePositionSelected.Text.Length == 0)
            {
                Error("请选择管道位置", this);
                TemplateNumberInput.Focus();
                return;
            }

            if (DesignPressureInput.Text.Length == 0)
            {
                Error("请输入设计压力", this);
                TemplateNumberInput.Focus();
                return;
            }

            if (EditEnableSelect.Text.Length == 0)
            {
                Error("请选择是否允许编辑", this);
                TemplateNumberInput.Focus();
                return;
            }
            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            obj.template_number = TemplateNumberInput.Text.ObjToInt();
            obj.pipe_position = PipePositionSelected.Text;
            obj.design_pressure = (float)Math.Round(float.Parse(DesignPressureInput.Text), 2);
            int edit = 0;
            if (EditEnableSelect.Text == "是")
            {
                edit = 1;
            }
            obj.edit_enable = edit;

            if (FirstStagePressureInput.Text.Length > 0 )
            {
                obj.first_stage_pressure = (float)Math.Round(float.Parse(FirstStagePressureInput.Text), 2);
            }
            if (FirstStageMinuteInput.Text.Length > 0)
            {
                obj.first_stage_minute = (float)Math.Round(float.Parse(FirstStageMinuteInput.Text), 2);
            }
            if (FirstStageEstimateSelect.Text.Length > 0)
            {
                obj.first_stage_estimate = FormatEstimate(FirstStageEstimateSelect.Text);
            }
            if (FirstStageEstimateValueInput.Text.Length > 0)
            {
                obj.first_stage_estimate_value = (float)Math.Round(float.Parse(FirstStageEstimateValueInput.Text), 3);
            }

            if (SecondStagePressureInput.Text.Length > 0)
            {
                obj.second_stage_pressure = (float)Math.Round(float.Parse(SecondStagePressureInput.Text), 2);
            }
            if (SecondStageMinuteInput.Text.Length > 0)
            {
                obj.second_stage_minute = (float)Math.Round(float.Parse(SecondStageMinuteInput.Text), 2);
            }
            if (SecondStageEstimateSelect.Text.Length > 0)
            {
                obj.second_stage_estimate = FormatEstimate(SecondStageEstimateSelect.Text);
            }
            if (SecondStageEstimateValueInput.Text.Length > 0)
            {
                obj.second_stage_estimate_value = (float)Math.Round(float.Parse(SecondStageEstimateValueInput.Text), 3);
            }

            if (ThirdStagePressureInput.Text.Length > 0)
            {
                obj.third_stage_pressure = (float)Math.Round(float.Parse(ThirdStagePressureInput.Text), 2);
            }
            if (ThirdStageMinuteInput.Text.Length > 0)
            {
                obj.third_stage_minute = (float)Math.Round(float.Parse(ThirdStageMinuteInput.Text), 2);
            }
            if (ThirdStageEstimateSelect.Text.Length > 0)
            {
                obj.third_stage_estimate = FormatEstimate(ThirdStageEstimateSelect.Text);
            }
            if (ThirdStageEstimateValueInput.Text.Length > 0)
            {
                obj.third_stage_estimate_value = (float)Math.Round(float.Parse(ThirdStageEstimateValueInput.Text), 3);
            }

            if (RigorStagePressureInput.Text.Length > 0)
            {
                obj.rigor_stage_pressure = (float)Math.Round(float.Parse(RigorStagePressureInput.Text), 2);
            }
            if (RigorStageMinuteInput.Text.Length > 0)
            {
                obj.rigor_stage_minute = (float)Math.Round(float.Parse(RigorStageMinuteInput.Text), 2);
            }
            if (RigorStageEstimateSelect.Text.Length > 0)
            {
                obj.rigor_stage_estimate = FormatEstimate(RigorStageEstimateSelect.Text);
            }
            if (RigorStageEstimateValueInput.Text.Length > 0)
            {
                obj.rigor_stage_estimate_value = (float)Math.Round(float.Parse(RigorStageEstimateValueInput.Text), 3);
            }

            if (IntensityStagePressureInput.Text.Length > 0)
            {
                obj.intensity_stage_pressure = (float)Math.Round(float.Parse(IntensityStagePressureInput.Text), 2);
            }

            if (IntensityStageMinuteInput.Text.Length > 0)
            {
                obj.intensity_stage_minute = (float)Math.Round(float.Parse(IntensityStageMinuteInput.Text), 2);
            }
            if (IntensityStageEstimateSelect.Text.Length > 0)
            {
                obj.intensity_stage_estimate = FormatEstimate(IntensityStageEstimateSelect.Text);
            }
            if (IntensityStageEstimateValueInput.Text.Length > 0)
            {
                obj.intensity_stage_estimate_value = (float)Math.Round(float.Parse(IntensityStageEstimateValueInput.Text), 3);
            }

            List<Template> list = new List<Template>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                if (this.Owner is not null)
                {
                    TemplateManageForm frm = (TemplateManageForm)Owner;
                    frm.LoadList();
                    string msg = string.Format("模板添加成功");
                    frm.Success(msg, frm);
                }
                this.Close();
            }
            else
            {
                 Error("添加模板失败", this);
            }
        }
        #endregion
    }
}
